package org.oa.ai.word;

import com.aspose.words.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class WordUtils {

    public static boolean getAsposeWordLicense(){
        boolean result = false;
        try {
            InputStream is =Thread.currentThread().getContextClassLoader().getResourceAsStream("aspose/aspose-license.xml"); // Context.getResourceAsStream("aspose-license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * html to word
     * @param body 要转换成word的 源html文档
     * @return 输出流
     */
    public static FileOutputStream HtmlToWord(String body, OutputStream out){
//        if(!getAsposeWordLicense()){
//            return null;
//        }
        // OutputStream out = null;
        try {
            //out = new FileOutputStream()
            //将HTML中的img标签的src全部转换为可访问到的地址（个人业务逻辑）
            //body = parseImgUrlFromBody(body)
            //这里是加入css样式，以引入的方式
//            String css = "<link rel='stylesheet' href='"+grailsApplication.config.staticFileBaseUrl+"/css/simditor.css' type='text/css' />";
            String css = "";
//            String cssFont = "<link rel='stylesheet' href='"+grailsApplication.config.staticFileBaseUrl+"/css/font-awesome.css' type='text/css' />";
            String cssFont = "";
            //拼接完整的HTML文档
            String html = "<!DOCTYPE html> <html> <head> "+css+cssFont+" </head> <body><div class='editor-style'>" + body + "</div></body> </html>";
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.insertHtml(html);
            doc.save(out, SaveOptions.createSaveOptions(SaveFormat.DOC));//生成doc文件
            return (FileOutputStream) out;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 将 Word 文档转换为 HTML
     *
     */
    public static String convertWordToHtmlString(MultipartFile file) throws Exception {
        try (InputStream inputStream = file.getInputStream()) {
            Document doc = new Document(inputStream);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            int options = SaveFormat.HTML;
            doc.save(output, options);
            return output.toString(StandardCharsets.UTF_8);
        }
    }

    public static void main(String[] args) throws FileNotFoundException {

        // 示例 HTML 内容
        String htmlBody = "<h1>示例标题</h1><p>这是段落内容。</p><img src='D:\\01.png' alt='示例图片' width='100'/>";

        // 目标输出文件路径
        String outputFilePath = "D:\\output.docx";

        try (FileOutputStream out = new FileOutputStream(outputFilePath)) {
            // 调用 HTML 转 Word 方法
            FileOutputStream result = HtmlToWord(htmlBody, out);

            if (result != null) {
                System.out.println("转换成功，文件已保存至: " + outputFilePath);
            } else {
                System.out.println("转换失败，许可证验证失败或发生异常。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
